/***************************************************************************
**                                                                        **
**  QCustomPlot, an easy to use, modern plotting widget for Qt            **
**  Copyright (C) 2011-2021 Emanuel Eichhammer                            **
**                                                                        **
**  This program is free software: you can redistribute it and/or modify  **
**  it under the terms of the GNU General Public License as published by  **
**  the Free Software Foundation, either version 3 of the License, or     **
**  (at your option) any later version.                                   **
**                                                                        **
**  This program is distributed in the hope that it will be useful,       **
**  but WITHOUT ANY WARRANTY; without even the implied warranty of        **
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
**  GNU General Public License for more details.                          **
**                                                                        **
**  You should have received a copy of the GNU General Public License     **
**  along with this program.  If not, see http://www.gnu.org/licenses/.   **
**                                                                        **
****************************************************************************
**           Author: Emanuel Eichhammer                                   **
**  Website/Contact: http://www.qcustomplot.com/                          **
**             Date: 29.03.21                                             **
**          Version: 2.1.0                                                **
****************************************************************************/

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    mPlot(0),
    mTag(0),
    size(0)
{
    ui->setupUi(this);

    mPlot = new QCustomPlot(this);
    setCentralWidget(mPlot);

    // configure plot to have two right axes:
    mPlot->yAxis->setTickLabels(false);
    mPlot->yAxis->setVisible(true);
    mPlot->yAxis2->setVisible(true);
    mPlot->axisRect()->addAxis(QCPAxis::atRight);
    mPlot->axisRect()->axis(QCPAxis::atRight, 0);//->setPadding(30); // add some padding to have space for tags

    // create graphs:
    mGraph1 = mPlot->addGraph(mPlot->xAxis, mPlot->axisRect()->axis(QCPAxis::atRight, 0));
    Graph.emplace_back(mGraph1);
//    mGraph2 = mPlot->addGraph(mPlot->xAxis, mPlot->axisRect()->axis(QCPAxis::atRight, 1));
    Graph[0]->setPen(QPen(QColor(250, 120, 0)));
//    mGraph2->setPen(QPen(QColor(0, 180, 60)));

    // create tags with newly introduced AxisTag class (see axistag.h/.cpp):
    mTag = new AxisTag(mGraph1->valueAxis());
    Tags.emplace_back(mTag);
    Tags[0]->setPen(Graph[0]->pen());

    mDataTimer.start(1);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::plot()
{
    while(size < buffer.size()){
        ++size;
        QPointer<QCPGraph> graph = mPlot->addGraph(mPlot->xAxis, mPlot->axisRect()->axis(QCPAxis::atRight, size));
        graph->setPen(QPen(QColor(0, 180, 60)));
        Graph.emplace_back(graph);

        AxisTag* tag = new AxisTag(graph->valueAxis());
        Tags.emplace_back(tag);
        Tags[size]->setPen(graph->pen());
    }

    for(int i = 0;i < size;++i)
    {
        // calculate and add a new data point to each graph:
        Graph[i]->addData(Graph[i]->dataCount(), *(buffer[i]));

        // make key axis range scroll with the data:
        mPlot->xAxis->rescale();
        Graph[i]->rescaleValueAxis(false, true);
        mPlot->xAxis->setRange(mPlot->xAxis->range().upper, 100, Qt::AlignRight);

        // update the vertical axis tag positions and texts to match the rightmost data point of the graphs:
        double graphValue = Graph[i]->dataMainValue(Graph[i]->dataCount()-1);
        Tags[i]->updatePosition(graphValue);
//        Tags[i]->setText("["+QString::number(i+1)+"]"+QString::number(graphValue, 'f', 2));
        Tags[i]->setText(QString::number(i+1));
    }
    mPlot->replot();
}
